package com.binvshe.binvshe.message.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.binvshe.binvshe.message.MessageType;
import com.binvshe.binvshe.message.dao.Message;
import com.binvshe.binvshe.message.dao.MessageHelper;
import com.pachong.android.baseuicomponent.CompState;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;

import java.util.List;

/**
 * 好友消息
 *
 * @Author: ${USER}
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: ${DATE} ${TIME}
 * @Version: 1.0.0
 */
public class FriendMessageFragment extends SwipeListFragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setPullToRefreshEnable(false);
        setAutoLoading(false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        reload();
    }

    void reload() {
        getAdapter().getData().clear();
        getAdapter().notifyDataSetChanged();
        startLoading();
    }


    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new FriendMsgListAdapter(getActivity());
    }

    @Override
    public void onStartLoading() {
        List<Message> list = MessageHelper.getInstance().loadMessage(MessageType.MSG_FRIEND_REQUEST);
        getData().addAll(list);
        getAdapter().notifyDataSetChanged();
        if (list.size() == 0) {
            setState(CompState.EMPTY);
        } else {
            setState(CompState.DATA);
            Message[] arr = list.toArray(new Message[list.size()]);
            MessageHelper.getInstance().updataIsRead(arr);
        }

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long itemId) {

        final AlertDialogFragment dialogFragment = new AlertDialogFragment();
        dialogFragment.setTitle("删除消息")
                .setMessage("确定删除？")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Message msg = (Message) getData().get(position);
                        MessageHelper.getInstance().getMsgDao().delete(msg);
                        reload();
                        dialogFragment.dismissAllowingStateLoss();
                    }
                })
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialogFragment.dismissAllowingStateLoss();
                    }
                }).show(getFragmentManager());

        return true;
    }
}
